﻿@using LeadMaster.Models
@model LeadMaster.ViewModels.ViewModelLeads

@{
AjaxOptions ajaxOpts = new AjaxOptions
    {
        HttpMethod = "Post"
    };
}
<style type="text/css">
    .TableCell
    {
        font-family: Arial, Helvetica, sans-serif; 
        font-size: 11px; 
        color: #808080;
    }
</style>
@using (Ajax.BeginForm("Edit", "Leads", null,ajaxOpts, new { @id="formEditLead"}))
{
@Html.ValidationSummary(true)
<fieldset style="border-style: none; width: 990px;">
    @Html.HiddenFor(model => model.Lead.ContactType)
    @Html.HiddenFor(model => model.CompanyID)
    @Html.HiddenFor(model => model.Lead.LeadID)
    @Html.HiddenFor(model => model.Lead.InitialContactDate)
    @Html.HiddenFor(model => model.Lead.ContactType)
    @Html.HiddenFor(model => model.UserFilter)
    @Html.HiddenFor(model => model.ColumnFilter)
    @Html.HiddenFor(model => model.Lead.ActionRequired)

    <input type="hidden" value="1" id="feeCounter" />
    <div id="leadedittabs">
    <ul>
    <li><a href="#tabs-1">Company</a></li>
    <li><a href="#tabs-2">Contact</a></li>
    <li><a href="#tabs-3">Commercials</a></li>
    </ul>
    <div id="tabs-1">
    <fieldset style="border-radius: 8px;">
        <div class="leftcolumn">
            @Html.HiddenFor(model => model.Company.CompanyID)

            <div class="editor-label">
                @Html.LabelFor(model => model.Company.Company)
            </div>
            <div class="editor-field">
                @Html.TextBoxFor(model => model.Company.Company, new { @class = "largetextbox" })
            </div>
            <div class="editor-label">
                @Html.Label("Int Ref")
            </div>
            <div class="editor-field">
                @Html.TextBoxFor(model => model.Lead.IntRef, new { @class = "forceuppercase largetextbox" })
                @Html.ValidationMessageFor(model => model.Lead.IntRef)
            </div>
            <div class="editor-label">
                @Html.LabelFor(model => model.Company.CoNumber)
            </div>
            <div class="editor-field">
                @Html.TextBoxFor(model => model.Company.CoNumber, new { @class = "largetextbox" })
                @Html.ValidationMessageFor(model => model.Company.CoNumber)
            </div>
            <div class="editor-label">
                @Html.LabelFor(model => model.Company.logo)
            </div>
            <div class="editor-field">
                @Html.TextBoxFor(model => model.Company.logo, new { @class = "largetextbox" })
                @Html.ValidationMessageFor(model => model.Company.logo)
            </div>
            <input id="btnRegistered" type="button" value="Registrations" class="smallbutton" style="visibility: hidden" />
            <input id="btnRestrictions" type="button" value="Restrictions" class="smallbutton" style="visibility: hidden"/>
            <input id="btnSundries" type="button" value="Sundries" class="smallbutton" style="visibility: hidden"/>
        </div>
        <div class="rightcolumn">
            <div class="editor-label" style="clear:both;">
                @Html.LabelFor(model => model.Company.Telephone)
            </div>
            <div>
                @Html.TextBoxFor(model => model.Company.Telephone, new { @class = "largetextbox" })
                @Html.RadioButtonFor(model => model.Company.DefaultTelephoneField, "Telephone")
                @Html.ValidationMessageFor(model => model.Company.Telephone)
            </div>
        
            <div class="editor-label">
                @Html.LabelFor(model => model.Company.Tel2)
            </div>
            <div>
                @Html.TextBoxFor(model => model.Company.Tel2, new { @class = "largetextbox" })
                @Html.RadioButtonFor(model => model.Company.DefaultTelephoneField, "Tel2")
                @Html.ValidationMessageFor(model => model.Company.Tel2)
            </div>
            <div class="editor-label">
                @Html.LabelFor(model => model.Company.Email)
            </div>
            <div class="editor-field">
                @Html.TextBoxFor(model => model.Company.Email, new { @class = "largetextbox" })
                @Html.ValidationMessageFor(model => model.Company.Email)
            </div>
            <div class="editor-label">
            @if (Model.Company.WWW!=null)
            {
                @:<a href="http://@(Model.Company.WWW)" target="_blank">@Html.Label("WWW", new { style="cursor:pointer"})</a>
            }
            else
            {
                @Html.LabelFor(model => model.Company.WWW)    
            }                
            </div>
            <div class="editor-field">
                @Html.TextBoxFor(model => model.Company.WWW, new { @class = "largetextbox" })
                @Html.ValidationMessageFor(model => model.Company.WWW)
            </div>
        </div>
        </fieldset>
        <div class="spacer" style="clear:both"></div>
        <div id="addresstabs">
        <ul>
        <li><a href="#RegisteredAddress">Registered Address</a></li>
        <li><a href="#TradingAddresses">Trading Addresses</a></li>
        </ul>
            <div id="RegisteredAddress">
                <div class="leftcolumn">
            <div class="editor-label">
                @Html.LabelFor(model => model.Company.Address1)
            </div>
            <div class="editor-field">
                @Html.TextBoxFor(model => model.Company.Address1, new { @class = "largetextbox" })
                @Html.ValidationMessageFor(model => model.Company.Address1)
            </div>

            <div class="editor-label">
                @Html.LabelFor(model => model.Company.Address2)
            </div>
            <div class="editor-field">
                @Html.TextBoxFor(model => model.Company.Address2, new { @class = "largetextbox" })
                @Html.ValidationMessageFor(model => model.Company.Address2)
            </div>

            <div class="editor-label">
                @Html.LabelFor(model => model.Company.District)
            </div>
            <div class="editor-field">
                @Html.TextBoxFor(model => model.Company.District, new { @class = "largetextbox" })
                @Html.ValidationMessageFor(model => model.Company.District)
            </div>
          </div>
        
                <div class="rightcolumn">
            <div class="editor-label">
                @Html.LabelFor(model => model.Company.Town)
            </div>
            <div class="editor-field">
                @Html.TextBoxFor(model => model.Company.Town, new { @class = "largetextbox" })
                @Html.ValidationMessageFor(model => model.Company.Town)
            </div>

            <div class="editor-label">
                @Html.LabelFor(model => model.Company.County)
            </div>
            <div class="editor-field">
                @Html.TextBoxFor(model => model.Company.County, new { @class = "largetextbox" })
                @Html.ValidationMessageFor(model => model.Company.County)
            </div>

            <div class="editor-label">
                @Html.LabelFor(model => model.Company.Postcode)
            </div>
            <div class="editor-field">
                @Html.TextBoxFor(model => model.Company.Postcode, new { @class = "forceuppercase largetextbox" })
                @Html.ValidationMessageFor(model => model.Company.Postcode)
            </div>
          </div>
                <div style="clear:both"></div>
            </div>
            <div id="TradingAddresses">

        <table>
        <tr>
            <th class="rowheader">@Html.LabelFor(model => model.TradingAddresses.First().TradingAddress1)</th>
            <td class="tradadd1" id="tradadd1_0">
            @{
            int taCount = 0;
            foreach (TradingAddresses item in Model.TradingAddresses)
            {
            @Html.HiddenFor(m => m.TradingAddresses[taCount].TradingAddressID)
            <input id="TradingAddresses_@(taCount.ToString())__TradingAddress1" name="TradingAddresses[@(taCount.ToString())].TradingAddress1" type="text" value="@(item.TradingAddress1)" />
                taCount++;
            }
            }
            </td>
        </tr>
        <tr>
            <th class="rowheader">@Html.LabelFor(model => model.TradingAddresses.First().TradingAddress2)</th>
            <td>
            @{
            taCount = 0;
            foreach (TradingAddresses item in Model.TradingAddresses)
            {
                <input id="TradingAddresses_@(taCount.ToString())__TradingAddress2" name="TradingAddresses[@(taCount.ToString())].TradingAddress2" type="text" value="@(item.TradingAddress2)" />
                taCount++;
            }
            }
            </td>
        </tr>
        <tr>
            <th class="rowheader">@Html.LabelFor(model => model.TradingAddresses.First().TradingDistrict)</th>
            <td>
            @{
            taCount = 0;
            foreach (TradingAddresses item in Model.TradingAddresses)
            {
                <input id="TradingAddresses_@(taCount.ToString())__TradingDistrict" name="TradingAddresses[@(taCount.ToString())].TradingDistrict" type="text" value="@(item.TradingDistrict)" />
                taCount++;
            }
            }            
            </td>
        </tr>
        <tr>
            <th class="rowheader">@Html.LabelFor(model => model.TradingAddresses.First().TradingTown)</th>
            <td>
            @{
            taCount = 0;
            foreach (TradingAddresses item in Model.TradingAddresses)
            {
                <input id="TradingAddresses_@(taCount.ToString())__TradingTown" name="TradingAddresses[@(taCount.ToString())].TradingTown" type="text" value="@(item.TradingTown)" />
                taCount++;
            }
            }            
            </td>
        </tr>
        <tr>
            <th class="rowheader">@Html.LabelFor(model => model.TradingAddresses.First().TradingCounty)</th>
            <td>
            @{
            taCount = 0;
            foreach (TradingAddresses item in Model.TradingAddresses)
            {
                <input id="TradingAddresses_@(taCount.ToString())__TradingCounty" name="TradingAddresses[@(taCount.ToString())].TradingCounty" type="text" value="@(item.TradingCounty)" />
                taCount++;
            }
            }            
            </td>
        </tr>
        <tr>
            <th class="rowheader">@Html.LabelFor(model => model.TradingAddresses.First().TradingPostcode)</th>
            <td>
            @{
            taCount = 0;
            foreach (TradingAddresses item in Model.TradingAddresses)
            {
                <input id="TradingAddresses_@(taCount.ToString())__TradingPostcode" name="TradingAddresses[@(taCount.ToString())].TradingPostcode" type="text" value="@(item.TradingPostcode)" class = "forceuppercase"/>
                taCount++;
            }
            }            
            </td>
        </tr>
        <tr>
            <th class="rowheader">@Html.Label("Default")</th>
            <td>
            @{
            taCount = 0;
            foreach (TradingAddresses item in Model.TradingAddresses)
            {
                <input id="DefaultTradingAddressID@(taCount.ToString())" name="DefaultTradingAddressID" style="width:125px;" type="radio" value="[@(taCount.ToString())]" 
                @if (Model.Lead.TradingAddressID == item.TradingAddressID)
                { 
                    @:checked 
                }  
                />
                taCount++;
            }
            }            
            </td>
        </tr>
        </table>
        <input type="button" value="add" id="addtrading@(--taCount)" class="addtrading" />
        </div>
        </div>
    </div>
    <div id="tabs-2">
        <div class="editor-field rightcolumn" style="text-align: right;">
            <img src="~/Images/diary.png" id="diaryicon" data-leadid="@(Model.Lead.LeadID)" style="width: 35px; height: 35px; cursor: pointer; vertical-align: middle;" alt="Diary Entry" />
        </div>
        <div style="clear:both;"></div>
        <div id="leadcontacts">
        @Html.Partial("ClientContactsList")
        </div>
        <input type="button" value="add" id="addcontact" />
    </div>
    <div id="tabs-3">
    <div id="commercialtabs">
        <ul>
        <li><a href="#Base">Base</a></li>
        <li><a href="#Details">Details</a></li>
        <li><a href="#Fees">Fee</a></li>
        <li><a href="#Communication">Communication</a></li>
        </ul>
        <div id="Base">
		<table style="width: 600px;">
        <tr>
        <th style="text-align: left">@Html.LabelFor(model => model.Sector.Sector, "Lead Type")</th>
        <td>
            @Html.DropDownList("SectorID", null, new { style = "width:300px;" })
            @Html.ValidationMessageFor(model => model.Sector.Sector)
        </td>
        </tr>
        <tr>
        <th style="text-align: left">@Html.LabelFor(model => model.Lead.LeadStatuses.LeadStatus)</th>
        <td>@Html.DropDownList("LeadStatusesID", null, new { style = "width:225px;" })
        </td>
        </tr>
		<tr>
        <th style="text-align: left; height: 30px;">@Html.LabelFor(model => model.Lead.Territory)</th>
        <td class="TableCell">
            @Html.TextAreaFor(model => model.Lead.Territory, new { @class = "forceuppercase notes" })
            @Html.ValidationMessageFor(model => model.Lead.Territory)</td>
        </tr>
      	<tr>
		<th style="text-align: left">@Html.LabelFor(model => model.Lead.CommencementDate, "Commencement Date")</th>
        <td>@Html.TextBoxFor(model => model.HoldingDate, new { @class = "datepicker", style = "width: 80px; font-family: Consolas; font-size: 12px" })</td>
        </tr>
        <tr>
        <th style="text-align: left; height: 30px;">@Html.LabelFor(model => model.Lead.TotalWeeks)</th>
        <td class="TableCell">@Html.TextBoxFor(model => model.Lead.TotalWeeks, new { @class = "largetextbox", style = "font-family: Consolas; font-size: 12px" })</td>
        </tr>
        <tr>
        <th style="text-align: left; height: 30px;">@Html.LabelFor(model => model.Lead.ReturnDays)</th>
        <td class="TableCell">@Html.TextBoxFor(model => model.Lead.ReturnDays, new { @class = "leadedit", style = "font-family: Consolas; font-size: 12px" })</td>
        </tr>
        <tr>
		<th style="text-align: left; height: 30px;">@Html.LabelFor(model => model.Lead.Guarantee)</th>
        <td class="TableCell">@Html.CheckBoxFor(model => model.Lead.Guarantee, new { @class = "largetextbox" })</td>
        </tr>
        <tr>
		<th style="text-align: left;height: 30px;">@Html.LabelFor(model => model.Lead.InvoiceInsuranceLimit)</th>
        <td class="TableCell">@Html.TextBoxFor(model => model.Lead.InvoiceInsuranceLimit, new { @class = "largetextbox", style = "font-family: Consolas; font-size: 12px" })</td>
		</tr>
        <tr>
        <th style="text-align: left">@Html.Label("Call Centre")</th>
        <td>
            @Html.ListBoxFor(m => Model.SelectedCallCentres, Model.CallCentres, new { style = "width:600px;", @class="chosen", multiple="true" })
        </td>
        </tr>
        <tr>
        <th style="text-align: left">@Html.Label("Lead Template")</th>
        <td>
            @Html.DropDownList("LeadTemplateID", null, new { style = "width:300px;" })
        </td>
        </tr>
		</table>
        <div class="spacer"></div>
        <table>
        <tr>
        <th style="text-align: left;height: 30px;">@Html.LabelFor(model => model.Lead.Combination)</th>
        <td>
        @Html.CheckBoxFor(m => m.Lead.Combination)
        </td>
        <th style="text-align: left;height: 30px;">@Html.LabelFor(model => model.Lead.CreditBased)</th>
        <td>
        @Html.CheckBoxFor(m => m.Lead.CreditBased)
        </td>
        <th style="text-align: left;height: 30px;">@Html.LabelFor(model => model.Lead.StreetView)</th>
        <td>
        @Html.CheckBoxFor(m => m.Lead.StreetView)
        </td>
        <th style="text-align: left;height: 30px;">@Html.LabelFor(model => model.Lead.EPC)</th>
        <td>
        @Html.CheckBoxFor(m => m.Lead.EPC)
        </td>
        <th style="text-align: left;height: 30px;">@Html.LabelFor(model => model.Lead.DunsmorePreCheck)</th>
        <td>
        @Html.CheckBoxFor(m => m.Lead.DunsmorePreCheck)
        </td>
        <th style="text-align: left;height: 30px;">@Html.LabelFor(model => model.Lead.NewLeadAlert)</th>
        <td>
        @Html.CheckBoxFor(m => m.Lead.NewLeadAlert)
        </td>
        </tr>
        </table>
        </div>
        <div id="Details">
        <table>
        @if (this.User.IsInRole(LeadMaster.Properties.ApplicationRoles.Operations))
        {
        <tr>
        <th style="text-align: left">@Html.LabelFor(model => model.Lead.ScheduleTemplateID, "Template")</th>
        <td>
            @Html.DropDownList("ScheduleTemplateID", null, new { style = "width:100%" })
            @Html.ValidationMessageFor(model => model.Lead.ScheduleTemplateID)
        </td>
        </tr>
        }
        @if (this.User.IsInRole(LeadMaster.Properties.ApplicationRoles.Operations))
        {
        <tr>
		<th style="text-align: left">@Html.LabelFor(model => model.Lead.TermConditionsID, "Terms & Conditions")</th>
        	<td>@Html.DropDownList("TermConditionsID", null, new { style = "width:100%" })
            @Html.ValidationMessageFor(model => model.Lead.TermConditionsID)</td>
        </tr>   
           
        <tr>
		<th style="text-align: left">@Html.LabelFor(model => model.Lead.LeadsReplacementID, "Lead Replacement")</th>
        		<td>@Html.DropDownList("LeadsReplacementID", null, new { style = "width:100%" })
            @Html.ValidationMessageFor(model => model.Lead.LeadsReplacementID)</td>
        </tr>   
        <tr>
		<th style="text-align: left">@Html.LabelFor(model => model.Lead.PaymentTermsID, "Payment Terms")</th>
        <td>@Html.DropDownList("PaymentTermsID", null, new { style = "width:100%" })
            @Html.ValidationMessageFor(model => model.Lead.PaymentTermsID)</td>
        </tr>
        <tr>
		<th style="text-align: left">@Html.LabelFor(model => model.Lead.InvoicingID, "Invoicing")</th>
  		<td>@Html.DropDownList("InvoicingID", null, new { style = "width:100%" })
            @Html.ValidationMessageFor(model => model.Lead.InvoicingID)</td>
		</tr>
        }
        <tr>
        <th>@Html.LabelFor(model => model.Lead.FreeNotes)</th>
        <td>
            @Html.TextAreaFor(model => model.Lead.FreeNotes, new { @class = "largenotes" })
            @Html.ValidationMessageFor(model => model.Lead.FreeNotes)
        </td>
        </tr>
        <tr>
        <th>@Html.LabelFor(model => model.Lead.bulletpoint)</th>
        <td>
            @Html.TextAreaFor(model => model.Lead.bulletpoint, new { @class = "largenotes" })
            @Html.ValidationMessageFor(model => model.Lead.bulletpoint)
        </td>
        </tr>
        </table>
        </div>
        <div id="Fees">
            <div class="editor-label">
                @Html.Label("Fees Other")
            </div>
            <div class="editor-field">
            @Html.TextAreaFor(model => model.Lead.FeesOther, new { @class = "notes" })
            @Html.ValidationMessageFor(model => model.Lead.FeesOther)
            </div>
            <div class="spacer"></div>
            <table>
            <tr>
            <th>
            @Html.LabelFor(m => m.newFee.Description)
            </th>
            <th>
            @Html.LabelFor(m => m.newFee.Quantity)
            </th>
            <th>
            @Html.LabelFor(m => m.newFee.PriceEach)
            </th>
            </tr>
            @{
        int iCounter = -1;
        foreach (Fees item in Model.TheFees)
        {
            iCounter++;
            @Html.HiddenFor(m => m.TheFees[iCounter].FeesID)
            <tr id="fee0">
                <td>
                @Html.TextBoxFor(m => m.TheFees[iCounter].Description, new { style = "width: 200px;" })
                </td>
                <td>
                @Html.TextBoxFor(m => m.TheFees[iCounter].Quantity, new { style = "width: 60px;" })
                </td>
                <td>
                £@Html.TextBoxFor(m => m.TheFees[iCounter].PriceEach, new { style = "width: 60px;" })
                </td>
            </tr>
        }
            }
            <tr id="fee@(iCounter)">
            </tr>
            </table>
        
            <input type="button" value="Add fee" id="btnAddFee@(iCounter)" class="addfee"/>
            <div style="clear:both;"></div>
        </div>
        <div id="Communication" style="height:480px; overflow:scroll;">
            @Html.Partial("~/Views/Enquiry/Index.cshtml", Model.Enquiries) 
            <div id="testenquiry"></div>
        </div>
        </div>
    </div>
    </div>
    </fieldset>
}

<input type="hidden" id="hdnNote" value="false" />

<div id="diarydialog" class="overlay">
</div>

<div id="companyregistrations"></div>
<div id="companyrestrictions"></div>
<div id="companysundries"></div>

<script type="text/javascript">
    $().ready(function () {

        @{
            if (Model.ContactID != 0)
            {
                <text>
                $( "#leadedittabs" ).tabs({ active: 1 });
                $( "#leadedittabs" ).tabs({ disabled: [0,2] });
                </text>
            }
            else if (Model.EditTab == 'S' || Model.EditTab == 'T')
            {
                <text>
                $( "#leadedittabs" ).tabs({ active: 2 });
                $( "#commercialtabs" ).tabs({ active: 0 });
                $( "#leadedittabs" ).tabs({ disabled: [0,1] });
                </text>
            }
            else if (Model.EditTab == 'F')
            {
                <text>
                $( "#leadedittabs" ).tabs({ active: 2 });
                $( "#commercialtabs" ).tabs({ active: 2 });
                $( "#leadedittabs" ).tabs({ disabled: [0,1] });
                </text>
            }
            else if (Model.EditTab == 'D')
            {
                <text>
                $( "#leadedittabs" ).tabs({ active: 2 });
                $( "#commercialtabs" ).tabs({ active: 3 });
                $( "#leadedittabs" ).tabs({ disabled: [0,1] });
                </text>
            }
            else if (Model.EditTab == 'U')
            {
                <text>
                $( "#leadedittabs" ).tabs({ active: 2 });
                $( "#leadedittabs" ).tabs({ disabled: [0,1] });
                </text>
            }
            else if (Model.EditTab == 'M')
            {
                <text>
                $( "#leadedittabs" ).tabs({ active: 0 });
                $( "#leadedittabs" ).tabs({ disabled: [1,2] });
                </text>
            }
            else if (Model.EditTab == 'Q')
            {
                <text>
                $( "#leadedittabs" ).tabs({ active: 2 });
                $( "#commercialtabs" ).tabs({ active: 3 });
                $( "#leadedittabs" ).tabs({ disabled: [0,1] });
                </text>
            }
        }

        //T-Territory
        //F-Fee
        //S-Status
        //D-Diary

        var currentDate = new Date();

        $(function () {
            $("#leadedittabs").tabs();
        });

        $(function () {
            $("#addresstabs").tabs();
        });

        $(function () {
            $("#commercialtabs").tabs();
        });

        $("#HoldingDate").datepicker({
            regional: 'en-GB',
            dateFormat: 'dd/mm/yy'
        });

        $('#LeadStatusesID').val("7");
        $('#PaymentTermsID').val(@(Model.Lead.PaymentTermsID));
        $('#InvoicingID').val(@(Model.Lead.InvoicingID));
        $('#ScheduleTemplateID').val(@(Model.Lead.ScheduleTemplateID));
        $('#TermConditionsID').val(@(Model.Lead.TermConditionsID));
        $("#CompanyID").attr('name', 'Lead.Company.CompanyID');
        $("#SectorID").attr('name', 'Sector.SectorID');
        $("#LeadsReplacementID").attr('name', 'Lead.LeadsReplacementID');
        $('#PaymentTermsID').attr('name', 'Lead.PaymentTermsID');
        $('#InvoicingID').attr('name', 'Lead.InvoicingID');
        $('#ScheduleTemplateID').attr('name', 'Lead.ScheduleTemplateID');
        $('#TermConditionsID').attr('name','Lead.TermConditionsID');
        $('#OwnerID').val("@(Model.Lead.UserID)");
        $('#LeadTemplateID').val(@(Model.Lead.LeadTemplateID));
        $("#LeadTemplateID").attr('name', 'Lead.LeadTemplateID');

        $("#CompanyID").change(function () {
            // update contact
            $.ajax({
                url: '@Url.Action("GetContactID", "Leads")/' + $(this).val(),
                type: "POST",
                contentType: 'application/json; charset=utf-8',
                processData: 'false',
                success: function (data) {
                    $("#ContactID").val(data);
                }
            });
            // update trading address
            $.ajax({
                url: '@Url.Action("TradingAddressID", "Leads")/' + $(this).val(),
                type: "POST",
                contentType: 'application/json; charset=utf-8',
                processData: 'false',
                success: function (data) {
                    $("#Lead_TradingAddressID").val(data);
                }
            });
        });

        $("#diarydialog").dialog(
        {
            autoOpen: false,
            width: 900,
            open: function () { $(".ui-dialog").css("box-shadow", "#000 5px 5px 5px"); },
            position: ['middle', 20],
            modal: true,
            dialogClass: 'notitle',
            buttons: {
                cancel: function () {
                    $(this).dialog("close");
                }
            }
        });

        $("#diaryicon").click(function () {
            var id = $(this).data("leadid");
            $("#diarydialog").dialog("open");
            $("#diarydialog").html('<div style="text-align: center;"><div><img src="@Url.Content("~/Images/ajaxspinner_large.gif")" alt="Loading..." style="margin: 20px;" /></div><div>Loading...</div></div>');
            $('#diarydialog').load('@(Url.Action("Create", "Diary"))/' + id);
            });

        $("#ContactID").change(function () {
            var thisval = $(this).val();
        });

        $(".addtrading").click(function () {
            var id = $(this).attr('id').substring(10);
            var newid = parseInt(id) + 1;
            var newObj = "<input id='TradingAddresses_" + newid.toString() + "__TradingAddress1' name='TradingAddresses[" + newid.toString() + "].TradingAddress1' type='text' value=''>";
            var currentObj = '#TradingAddresses_' + id.toString() + '__TradingAddress1'
            $(newObj).insertAfter(currentObj);
            newObj = "<input id='TradingAddresses_" + newid.toString() + "__TradingAddress2' name='TradingAddresses[" + newid.toString() + "].TradingAddress2' type='text' value=''>";
            currentObj = '#TradingAddresses_' + id.toString() + '__TradingAddress2';
            $(newObj).insertAfter(currentObj);
            newObj = "<input id='TradingAddresses_" + newid.toString() + "__TradingDistrict' name='TradingAddresses[" + newid.toString() + "].TradingDistrict' type='text' value=''>";
            currentObj = '#TradingAddresses_' + id.toString() + '__TradingDistrict';
            $(newObj).insertAfter(currentObj);
            newObj = "<input id='TradingAddresses_" + newid.toString() + "__TradingTown' name='TradingAddresses[" + newid.toString() + "].TradingTown' type='text' value=''>";
            currentObj = '#TradingAddresses_' + id.toString() + '__TradingTown';
            $(newObj).insertAfter(currentObj);
            newObj = "<input id='TradingAddresses_" + newid.toString() + "__TradingCounty' name='TradingAddresses[" + newid.toString() + "].TradingCounty' type='text' value=''>";
            currentObj = '#TradingAddresses_' + id.toString() + '__TradingCounty';
            $(newObj).insertAfter(currentObj);
            newObj = "<input id='TradingAddresses_" + newid.toString() + "__TradingPostcode' name='TradingAddresses[" + newid.toString() + "].TradingPostcode' type='text' value=''>";
            currentObj = '#TradingAddresses_' + id.toString() + '__TradingPostcode';
            $(newObj).insertAfter(currentObj);
            newObj = "<input id='DefaultTradingAddressID" + newid.toString() + "' name='DefaultTradingAddressID' type='radio' value='[" + newid.toString() + "]' style='width:125px;'>";
            currentObj = '#DefaultTradingAddressID' + id.toString().replace('0', '');
            $(newObj).insertAfter(currentObj);

            $(".addtrading").attr('id', 'addtrading' + newid.toString());
        });

        $("#addcontact").click(function () {
            $('#leadcontacts').html('<div style="text-align: center;"><div><img src="@Url.Content("~/Images/ajaxspinner_large.gif")" alt="Loading..." style="margin: 20px;" /></div><div>Loading...</div></div>');
            var contactURL='@(Url.Action("AddContact", "Leads"))/?LeadID=' + @(Model.Lead.LeadID);
            $('#leadcontacts').load(contactURL);
        });

        $(".addfee").click(function () {
            var id = $(this).attr('id').substring(9);
            var newid = parseInt(id) + 1;
            var newObj = "<tr id='fee" + newid.toString() + "'><td><input id='TheFees_" + newid.toString() + "__Description' name='TheFees[" + newid.toString() + "].Description' style='width: 200px;' type='text' value=''></td><td><input id='TheFees_" + newid.toString() + "__Quantity' name='TheFees[" + newid.toString() + "].Quantity' style='width: 60px;' type='text' value='1'></td><td>£<input id='TheFees_" + newid.toString() + "__PriceEach' name='TheFees[" + newid.toString() + "].PriceEach' style='width: 60px;' type='text' value='0.0'></td></tr>";
            var currentObj = "#fee" + id.toString();
            $(newObj).insertAfter(currentObj);
            $(this).attr('id', 'btnAddFee' + newid.toString());
        });

        $("#ContactID").change(function() {
            $("#Contact_Forename").val("");
            $("#Contact_Surname").val("");
            $("#Contact_Title").val("");
            $("#Contact_Email").val("");
            $("#Contact_skype").val("");
            $("#Contact_notes").val("");
            $("#Contact_Telephone").val("");
            $("#Contact_Tel2").val("");
            $("#Contact_Mobile1").val("");
            $("#Contact_Mobile2").val("");
            $("#Contact_linkedin").val("");
            $("#Contact_googleid").val("");
        });

        $("#btnRegistered").click(function() {
            $('#companyregistrations').dialog("open");
            $('#companyregistrations').html('<div style="text-align: center;"><div><img src="@Url.Content("~/Images/ajaxspinner_large.gif")" alt="Loading..." style="margin: 20px;" /></div><div>Loading...</div></div>');
            var contactURL='@(Url.Action("CompanyRegistrations", "Leads"))/?CompanyID=' + @(Model.Lead.CompanyID);
            $('#companyregistrations').load(contactURL);
        });

        $("#btnRestrictions").click(function() {
            $('#companyrestrictions').dialog("open");        
            $('#companyrestrictions').html('<div style="text-align: center;"><div><img src="@Url.Content("~/Images/ajaxspinner_large.gif")" alt="Loading..." style="margin: 20px;" /></div><div>Loading...</div></div>');
            var contactURL='@(Url.Action("CompanyRestrictions", "Leads"))/?CompanyID=' + @(Model.Lead.CompanyID);
            $('#companyrestrictions').load(contactURL);
        });
        $("#btnSundries").click(function() {
            $('#companysundries').dialog("open");
            $('#companysundries').html('<div style="text-align: center;"><div><img src="@Url.Content("~/Images/ajaxspinner_large.gif")" alt="Loading..." style="margin: 20px;" /></div><div>Loading...</div></div>');
            var contactURL='@(Url.Action("CompanySundries", "Leads"))/?CompanyID=' + @(Model.Lead.CompanyID);
            $('#companysundries').load(contactURL);
        });

        $("#companyregistrations").dialog(
        {
            autoOpen: false,
            width: 1100,
            open: function () { $(".ui-dialog").css("box-shadow", "#000 5px 5px 5px"); },
            position: ['middle', 20],
            dialogClass: 'notitle',
            buttons: {
                cancel: function () {
                    $(this).dialog("close");
                }
            }
        });

        $("#companyrestrictions").dialog(
        {
            autoOpen: false,
            width: 900,
            open: function () { $(".ui-dialog").css("box-shadow", "#000 5px 5px 5px"); },
            position: ['middle', 20],
            dialogClass: 'notitle',
            buttons: {
                cancel: function () {
                    $(this).dialog("close");
                }
            }
        });

        $("#companysundries").dialog(
        {
            autoOpen: false,
            width: 900,
            open: function () { $(".ui-dialog").css("box-shadow", "#000 5px 5px 5px"); },
            position: ['middle', 20],
            dialogClass: 'notitle',
            buttons: {
                cancel: function () {
                    $(this).dialog("close");
                }
            }
        });

        $(".chosen").chosen();
     });    
</script>